// pages/goodsManagement/goodsManagement.js
var app = getApp()
var WXAPI = require('./../../wxApi/main')
const store = require('./../../utils/store')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    manage: false,
    selectAll: false,
    selectList: new Array,
    goodsList:[],
    modalName: '',
    imgBaseUrl: app.globalData.imgBaseUrl
  }, 
  // 商品管理
  management(){
    this.setData({
      manage : true
    });
  } ,
  //上架
  up(e){
    let dataset = e.currentTarget.dataset
    if(dataset.id){
      WXAPI.upOrDown({
        id: dataset.id,
        status:1
      }).then(res=>{
        if(res.code == 200){
          wx.showToast({
            title: '商品上架成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(() => {
            this.onLoad();
          }, 2000);
        }else{
          wx.showToast({
            title: '商品上架失败',
            icon: 'none',
            duration: 3000
          })
        }
      }).catch(err=>{
        console.log(err)
      })
      return;
    }
  },
  //下架
  down(e){
    let dataset = e.currentTarget.dataset
    if(dataset.id){
      WXAPI.upOrDown({
        id: dataset.id,
        status:2
      }).then(res=>{
        if(res.code == 200){
          wx.showToast({
            title: '商品下架成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(() => {
            this.onLoad();
          }, 2000);
        }else{
          wx.showToast({
            title: '商品下架失败',
            icon: 'none',
            duration: 3000
          })
        }
      }).catch(err=>{
        console.log(err)
      })
      return;
    }
  },
  //置顶
  top(e){
    let dataset = e.currentTarget.dataset
    if(dataset.id){
      WXAPI.upOrDown({
        id: dataset.id,
        status:3
      }).then(res=>{
        if(res.code == 200){
          wx.showToast({
            title: '商品置顶成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(() => {
            this.onLoad();
          }, 2000);
        }else{
          wx.showToast({
            title: '商品置顶失败',
            icon: 'none',
            duration: 3000
          })
        }
      }).catch(err=>{
        console.log(err)
      })
      return;
    }
  },
  // 删除操作
  delGoods(e){
    // deleteBatch
    let dataset = e.currentTarget.dataset
    if(dataset.id){
      WXAPI.deleteGoods({
        id: dataset.id
      }).then(res=>{
        if(res.code == 200){
          wx.showToast({
            title: '商品删除成功',
            icon: 'success',
            duration: 3000
          })
          setTimeout(() => {
            this.onLoad();
          }, 2000);
        }else{
          wx.showToast({
            title: '商品删除失败',
            icon: 'none',
            duration: 3000
          })
        }
      }).catch(err=>{
        console.log(err)
      })
      return;
    }
    // 批量删除操作
    let ids = this.data.selectList.join(',')
    WXAPI.deleteBatch({
      ids: ids
    }).then(res=>{
      if(res.code == 200){
        wx.showToast({
          title: '商品删除成功',
          icon: 'success',
          duration: 3000
        })
        setTimeout(() => {
          this.onLoad();
        }, 2000);
      }else{
        wx.showToast({
          title: '商品删除失败',
          icon: 'none',
          duration: 3000
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  } ,
  //单选
  select: function (e) {
    let selectId = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index;
    let newli = 'goodsList[' + index + '].check';
    var selectList = this.data.selectList
    this.setData({
      [newli]: !this.data.goodsList[index].check
    })
    let num = 0;
    for(var i=0; i<this.data.goodsList.length; i++){
      if(this.data.goodsList[i].check){
        if(selectList.indexOf(this.data.goodsList[i].id) == -1){
          selectList.push(this.data.goodsList[i].id)
        }
        num ++;
        this.setData({
          selectList: selectList
        })
      }
    }
    console.log(newli)
    if(num == this.data.goodsList.length){
      this.setData({
        selectAll: true
      })
    }else{
      this.setData({
        selectAll: false
      })
    }
  },
  //全选，取消全选
  selectAll: function (e) {
    let list = this.data.goodsList;
    let selectAll = this.data.selectAll;
    var selectList = this.data.selectList;
    if (selectList.length < 1) {
      for (let i = 0; i < list.length; i++) {
        let newli = 'goodsList[' + i + '].check';
        selectList.push(list[i].id)
        this.setData({
          [newli]: true,
          selectAll: true,
          selectList: selectList
        })
      }
      console.log(this.data.selectList)
    }else{
      for (let i = 0; i < list.length; i++) {
        let newli = 'goodsList[' + i + '].check'
        this.setData({
          [newli]: false,
          selectAll: false,
          manage: false , // 退出管理
          selectList: [],
        })
      }
    }
  },
  
  // 模态框控制
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  // 获取商品列表
  getGoodsList(){
    // goodsInfo
    WXAPI.goodsList().then(res=>{
      wx.hideLoading()
      this.setData({
        modalName: null,
        manage: false
      })
      if(res.code == 200){
        let data =res.data
        data.forEach(element => {

          if(element.img.indexOf(',')){
            element.img=element.img.split(',')[0]
          }

          if(element.checked==1){
            element.checked='审核中'
            element.color='red'
          }else if(element.checked==2){
            element.checked='审核通过'
            element.color='green'
          }else{
            element.checked='未通过审核'
            element.color='yellow'
          }

          if(element.status==1){
            element.status='已上架'
            element.scolor='red'
          }else if(element.status==2){
            element.status='已下架'
            element.scolor='green'
          }else{
            element.status='已置顶'
            element.scolor='yellow'
          }
        });
        this.setData({
          goodsList: data
        })
      }else{
        wx.showToast({
          title: '数据加载失败...',
          icon: 'none',
          duration: 3000
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!store.getItem("userInfo")){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    wx.showLoading({
      title: '数据加载中...',
      mask: true
    })
    this.getGoodsList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})